<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Upload extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		
        $config['upload_path']      = './uploads/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 100;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;

        //生成一个Md5字符串
        $fileMd5Name=md5(microtime(),false);

        $config['file_name']  = $fileMd5Name;

        $this->load->library('upload', $config);

        $this->upload->do_upload('file');

        $fileData=$this->upload->data();

        $data=array(
        	'fileName'=>$fileData['file_name'],
        	'fileType'=>$fileData['file_type'],
        	'fileSize'=>$fileData['file_size'],
        	'imgWidth'=> $fileData['image_width'],
   			'imgHeight' =>$fileData ['image_height']
        );
        echo json_encode($data);

	}


	
	/**
	* @method upload_logo
	* @argument $config 配置项
	* @return  
	*/
	public function upload_logo()
	{
		
        //生成一个Md5字符串
        $fileMd5Name=md5(microtime(),false);
		
		/**
		* 取生成的文件名第一个字符，对字符进行16进制转10进制，
		* 模4，分配到4个目录下，方便后期拆分
		*/
		$fileNameInitial=$fileMd5Name[0];

		
		//16进制转10进制，模4,得到分配到的目录id
		$dirId=hexdec($fileNameInitial)%4?hexdec($fileNameInitial)%4:4;
		//分配到的目录
		$dirName='img'.$dirId.'/';
		//对应到服务器上的子域名
		$serverName='//img'.$dirId.'.uiimage.com/';
		
		
		//配置上传选项
		$config['file_name']  = $fileMd5Name;
		$config['upload_path']      = './uploads/'.$dirName;
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 100;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;
		
		
		//载入上传配置，初始化上传类库
        $this->load->library('upload', $config);
		//上传结果 true or false
		$uploadResult=$this->upload->do_upload('logo');
		
	
		//上传成功
		if($uploadResult){
			//文件上传相关数据
			$fileData=$this->upload->data();
			//文件的url路径
			$serverPath=$serverName.$fileData['file_name'];

			$data=array(
				'status'=>'success',
				'code'=>200,
				'data'=>array(
					'filePath'=>$serverPath,
					'fileName'=>$fileData['file_name'],
					'fileType'=>$fileData['file_type'],
					'fileSize'=>$fileData['file_size'],
					'imgWidth'=> $fileData['image_width'],
					'imgHeight' =>$fileData ['image_height']
				),
				
			);
		}else{
			//上传失败
			$data=array(
				'status'=>'error',
				'code'=>400,
				'data'=>array()
			);
		}

        
        echo json_encode($data);
	}

	public function upload_poster($pid)
	{
		
		$config['upload_path']      = './uploads/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 100;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;

        //生成一个Md5字符串
        $fileMd5Name=md5(microtime(),false);

        $config['file_name']  = $fileMd5Name;

        $this->load->library('upload', $config);

        $this->upload->do_upload('file');

        $fileData=$this->upload->data();

        $data=array(
        	'fileName'=>$fileData['file_name'],
        	'fileType'=>$fileData['file_type'],
        	'fileSize'=>$fileData['file_size'],
        	'imgWidth'=> $fileData['image_width'],
   			'imgHeight' =>$fileData ['image_height']
        );
        echo json_encode($data);
	}

	
	/**
	* @method upload_file
	* @argument $config 配置项
	* @return  
	*/
	public function upload_file()
	{
		
		

        //生成一个Md5字符串
        $fileMd5Name=md5(microtime(),false);
		
		/**
		* 取生成的文件名第一个字符，对字符进行16进制转10进制，
		* 模4，分配到4个目录下，方便后期拆分
		*/
		$fileNameInitial=$fileMd5Name[0];

		
		//16进制转10进制，模4,得到分配到的目录id
		$dirId=hexdec($fileNameInitial)%4?hexdec($fileNameInitial)%4:4;
		//分配到的目录
		$dirName='img'.$dirId.'/';
		//对应到服务器上的子域名
		$serverName='//img'.$dirId.'.uiimage.com/';
		
		
		//配置上传选项
		$config['file_name']  = $fileMd5Name;
		$config['upload_path']      = './uploads/'.$dirName;
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 100;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;
		
        

		//载入上传配置，初始化上传类库
        $this->load->library('upload', $config);
		//上传结果 true or false
		$uploadResult=$this->upload->do_upload('upload');
		
		

		//上传成功
		if($uploadResult){
			//文件上传相关数据
			$fileData=$this->upload->data();
			//文件的url路径
			$serverPath=$serverName.$fileData['file_name'];
		
			$data=array(
				'status'=>'success',
				'code'=>200,
				'data'=>array(
					'filePath'=>$serverPath,
					'fileName'=>$fileData['file_name'],
					'fileType'=>$fileData['file_type'],
					'fileSize'=>$fileData['file_size'],
					'imgWidth'=> $fileData['image_width'],
					'imgHeight' =>$fileData ['image_height']
				),
				
			);
			
		}else{
			//上传失败
			$data=array(
				'status'=>'error',
				'code'=>400,
				'data'=>array()
			);
		}

        //输出上传结果
        echo json_encode($data);

	}
	
	public function upload_umeditor_file(){
		//生成一个Md5字符串
        $fileMd5Name=md5(microtime(),false);
		
		/**
		* 取生成的文件名第一个字符，对字符进行16进制转10进制，
		* 模4，分配到4个目录下，方便后期拆分
		*/
		$fileNameInitial=$fileMd5Name[0];

		
		//16进制转10进制，模4,得到分配到的目录id
		$dirId=hexdec($fileNameInitial)%4?hexdec($fileNameInitial)%4:4;
		//分配到的目录
		$dirName='img'.$dirId.'/';
		//对应到服务器上的子域名
		$serverName='//img'.$dirId.'.uiimage.com/';
		
		
		//配置上传选项
		$config['file_name']  = $fileMd5Name;
		$config['upload_path']      = './uploads/'.$dirName;
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 100;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;
		
        

		//载入上传配置，初始化上传类库
        $this->load->library('upload', $config);
		//上传结果 true or false
		$uploadResult=$this->upload->do_upload('upfile');
		
		

		//上传成功
		if($uploadResult){
			//文件上传相关数据
			$fileData=$this->upload->data();
			//文件的url路径
			$serverPath=$serverName.$fileData['file_name'];
		
			$data=array(
				'status'=>'success',
				'code'=>200,
				'state'=>'SUCCESS',
				'url' => $serverPath,
				'title'=>$fileData['file_name'],
				'type'=>$fileData['file_type'],
				'size'=>$fileData['file_size'],
				'imgWidth'=> $fileData['image_width'],
				'imgHeight' =>$fileData ['image_height']
			);
			
		}else{
			//上传失败
			$data=array(
				'status'=>'error',
				'code'=>400,
				'state'=>'ERROR',
			);
		}

        //
        //umeditor支持的返回结果
		/*
		{
			"state": "SUCCESS",
			"url": "upload/demo.jpg",
			"title": "demo.jpg",
			"original": "demo.jpg"
		}
		*/
		
		echo json_encode($data);
	}
	
}
